package com.cskaoyan.service.system;

import com.cskaoyan.bean.system.Log;
import com.cskaoyan.bean.system.LogExample;
import com.cskaoyan.bo.system.LogInsertBo;
import com.cskaoyan.bo.system.ListWithNameBo;
import com.cskaoyan.mapper.LogMapper;
import com.cskaoyan.vo.system.LogListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    LogMapper logMapper;

    @Override
    public void insertLog(LogInsertBo logInsertBo) {
        System.out.println("_____________________" + logInsertBo);
        logMapper.insertLog(logInsertBo);
    }

    @Override
    public LogListVo listLogs(ListWithNameBo listWithNameBo) {

        PageHelper.startPage(listWithNameBo.getPage(), listWithNameBo.getLimit());

        LogExample logExample = new LogExample();
        logExample.setOrderByClause(listWithNameBo.getSort() + " " + listWithNameBo.getOrder());
        LogExample.Criteria criteria = logExample.createCriteria();
        if (listWithNameBo.getName() != null && "".equals(listWithNameBo.getName())) {
            criteria.andAdminLike("%" + listWithNameBo.getName() + "%");
        }
        List<Log> logList = logMapper.selectByExample(logExample);
        PageInfo<Log> logPageInfo = new PageInfo<>(logList);
        long total = logPageInfo.getTotal();

        LogListVo logListVo = new LogListVo();
        logListVo.setItems(logList);
        logListVo.setTotal(total);

        return logListVo;
    }
}
